<template>
	<view class="person-msg">
		<view class="head" @click="changeFace">
			<view class="avatar-box">
				<image :src="form.face || '/static/img/default-avatar.png'" mode="aspectFill"></image>
			</view>
		</view>
		<u-form :model="form" :error-type="['toast']" ref="uForm" class="form">
			<u-form-item label="昵称" prop="nickName" label-width="150">
				<u-input v-model="form.nickName" height="136" placeholder="仅限中文汉字、数字、英文字母" :maxlength="15" />
			</u-form-item>
			<u-form-item class="item-sex" label="性别" label-width="150">
				<u-radio-group prop="sex" v-model="form.sex" :active-color="lightColor">
					<u-radio name="1">男</u-radio>
					<u-radio name="0">女</u-radio>
					<u-radio name="2">保密</u-radio>
				</u-radio-group>
			</u-form-item>

			<u-form-item label="生日" label-width="150" right-icon="arrow-right">
				<u-input @click="showBirthday = true" disabled v-model="form.birthday" height="136"
					placeholder="请选择你的出生日期" />
				<u-picker v-model="showBirthday" mode="time" :confirm-color="lightColor"
					@confirm="selectTime"></u-picker>
			</u-form-item>

			<!-- 	  <u-form-item label="手机号" label-width="150">
		<view v-if="form.mobile">
			{{form.mobile}}
		</view>
		<view v-else>
			<view class="submit" @click="navigateTo(form.username)">绑定手机号码</view>
		</view>
	  </u-form-item> -->

		</u-form>
		<view class="bottom">
			<view class="submit" @click="submit">保存</view>
		</view>

	</view>
</template>
<script>
	import {
		saveUserInfo,
		getUserInfo
	} from "@/api/members.js";
	import {
		upload
	} from "@/api/common.js";
	import storage from "@/utils/storage.js";
	import uFormItem from "@/uview-ui/components/u-form-item/u-form-item.vue";
	export default {
		components: {
			uFormItem,
		},
		data() {
			return {
				lightColor: this.$lightColor, //高亮颜色
				form: {
					nickName: storage.getUserInfo().nickName || "",
					birthday: storage.getUserInfo().birthday || "",
					face: storage.getUserInfo().face || "/static/img/default-avatar.png", //默认头像
					sex: storage.getUserInfo().sex || '1', //性别
					mobile: storage.getUserInfo().mobile,
					username: storage.getUserInfo().username,
				},
				birthday: storage.getUserInfo().birthday || "", //生日
				showBirthday: false, //显示生日日期
				rules: {
					nickName: [
						// 必填规则
						{
							required: true,
							message: '【昵称】不能为空',
							// blur和change事件触发检验
							trigger: ['blur', 'change'],
						},
						// 正则判断为字母或数字
						{
							pattern: /^[\u4e00-\u9fa5A-Za-z0-9]*$/g,
							// 正则检验前先将值转为字符串
							transform(value) {
								return String(value);
							},
							message: '【昵称】仅限中文汉字、数字、英文字母'
						},
						// 6-8个字符之间的判断
						{
							min: 1,
							max: 15,
							message: '【昵称】仅支持1-15个字符'
						}
					]
				}
			};
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			/**
			 * 提交保存
			 */
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						let params = JSON.parse(JSON.stringify(this.form));
						saveUserInfo(params).then((res) => {
							if (res.statusCode == 200) {
								storage.setUserInfo(res.data.result);
								uni.navigateBack();
							}
						});
					} else {
						console.log('验证失败');
					}
				});

			},

			/**
			 * 修改头像
			 */
			changeFace(index) {
				uni.chooseImage({
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						uni.uploadFile({
							url: upload,
							filePath: tempFilePaths[0],
							name: "file",
							header: {
								accessToken: storage.getAccessToken(),
							},
							success: (uploadFileRes) => {
								let data = JSON.parse(uploadFileRes.data);

								this.form.face = data.result;
							},
						});
					},
				});
			},


			/**
			 * 选择时间
			 */
			selectTime(time) {
				this.form.birthday = `${time.year}-${time.month}-${time.day}`;
				this.birthday = `${time.year} - ${time.month} - ${time.day}`;
			},

			navigateTo(username) {
				uni.navigateTo({
					url: '/pages/mine/set/securityCenter/bindMobile' + '?username=' + username,
				});
			},
		},

		/**
		 * 加载数据
		 */
		onLoad() {},
	};
</script>
<style lang="scss" scoped>
	.avatar-box {
		position: relative;

		&::after {
			display: block;
			width: 56rpx;
			height: 56rpx;
			content: '';
			background-image: url('@/static/img/camera.png');
			background-size: cover;
			background-repeat: no-repeat;
			position: absolute;
			right: 0;
			bottom: 0;
		}
	}

	.submit {
		height: 90rpx;
		line-height: 90rpx;
		text-align: center;
		margin-top: 90rpx;

		width: 100%;
		margin: 0 auto;
		color: $main-color;
		border-radius: 100px;
	}

	.head {
		// background-color: #F5F5F5;
		// height: 260rpx;
		// color: $font-color-light;
		// font-size: $font-sm;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		// line-height: 2em;
		margin: 60rpx 0 66rpx 0;

		image {
			width: 190rpx;
			height: 190rpx;
			border-radius: 50%;
		}
	}

	/deep/ .u-form {
		background-color: #ffffff;
		padding: 0;
		margin-top: 30rpx;

		.item-sex {
			.u-form-item {
				height: 136rpx;
				display: flex;
				justify-content: center;
			}
		}

		.u-form-item {
			padding: 0 20rpx;
		}
	}

	.form {
		background-color: #ffffff;
	}

	.bottom {
		margin-top: 152rpx;
		display: flex;
		align-items: center;
		width: 100%;

		>.submit {
			background: $light-color;
			color: #fff;
			width: 370rpx;
			height: 72rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: 36rpx;
		}

	}

	.light {
		background: rgba($color: $light-color, $alpha: 0.2) !important;
		color: $light-color !important;
	}
</style>